package com.andyliu.system.service.impl;

import com.andyliu.system.dto.MenuDto;
import com.andyliu.system.entity.Menu;
import com.andyliu.system.entity.Ztree;
import com.andyliu.system.mapper.MenuMapper;
import com.andyliu.system.service.IMenuService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author andyLiu
 * @since 2021-08-10
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;


      public  IPage<Menu> selectByPage(Integer pageNumber,Integer pageSize, Menu menu){
        return menuMapper
                .selectByPage(new Page<Menu>(pageNumber,pageSize),menu);
      }

    @Override
    public IPage<MenuDto> selectByPage_(Integer pageNumber, Integer pageSize, MenuDto menu) {
        return menuMapper.selectByPage(new Page<MenuDto>(pageNumber,pageSize),menu);
    }

    @Override
    public List<Ztree> menuTreeData() {
        List<Menu> menuList = this.list();
        List<Ztree> ztrees = initZtree(menuList);
        return ztrees;
    }

    /**
     * 对象转菜单树
     *
     * @param menuList 菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<Menu> menuList)
    {
        return initZtree(menuList, null, false);
    }

    /**
     * 对象转菜单树
     *
     * @param menuList 菜单列表
     * @param roleMenuList 角色已存在菜单列表
     * @param permsFlag 是否需要显示权限标识
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<Menu> menuList, List<String> roleMenuList, boolean permsFlag)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = (roleMenuList==null?false:true);
        for (Menu menu : menuList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(menu.getMenuId());
            ztree.setpId(menu.getParentId());
            ztree.setName(transMenuName(menu, permsFlag));
            ztree.setTitle(menu.getMenuName());
            if (isCheck)
            {
                ztree.setChecked(roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }
    public String transMenuName(Menu menu, boolean permsFlag)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(menu.getMenuName());
        if (permsFlag)
        {
            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPerms() + "</font>");
        }
        return sb.toString();
    }

    @Override
    public MenuDto getById(Integer menuId) {
        return menuMapper.selectMenuById(menuId);
    }

    @Override
    public boolean saveRoleMenus(Integer roleId, Integer[] ids) {
        menuMapper.deleteRoleMenus(roleId);
        Map map = new HashMap();

        map.put("roleId",roleId);
        map.put("ids",ids);
        int flag = menuMapper.insertRoleMenus(map);
        return flag==ids.length?true:false;
    }

    @Override
    public List<MenuDto> listRoleMenus(Integer roleId) {
        return menuMapper.selectByRoleId(roleId);
    }

    @Override
    public List<MenuDto> listRoleMenusForIndex(Integer roleId) {
        return menuMapper.selectRoleMenus(roleId);
    }
}
